<?php

/**
 * Handles sms price queries
 */
class PriceInquiryAction extends CAction {

    public function run() {
        $senderNumber = Yii::app()->request->getQuery('senderNumber');
        $keyword = Yii::app()->request->getQuery('keyword');
        $message = Yii::app()->request->getQuery('message');
        $pattern = '/^[a-zA-Z]+ [0-9]+$/';

        if ($senderNumber && strtoupper($keyword) == 'PRICE' && preg_match($pattern, strtoupper($message))) {
            list($cropType, $groupId) = explode(' ', $message);

            if ($prices = Price::model()->queryRecent($cropType, $groupId)) {
                $message = $this->controller->renderPartial('_sms_prices', array(
                    'data' => $prices
                        ), true);

                Yii::app()->sms->send($senderNumber, $message);
            } else {
                Yii::app()->sms->send($senderNumber, 'No records found.');
            }
        } else {
            // invalid keyword error.
            Yii::app()->sms->send($senderNumber, 'Your message cannot be processed. For more information, Please type HELP and send.');
        }
    }

}

?>
